﻿using ByHand.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;

namespace ByHand.Services.Code
{
    public class Utility
    {
        public static string GetValueDynamic(object obj, string propertyname)
        {
            return obj.GetType().GetProperty(propertyname).GetValue(obj, null).ToString();
        }
        public static string GenerateProductCode(string providecode, int productid)
        {
            string id = productid.ToString();
            string output = providecode;
            if (id.Length <= 7)
            {
                var zero = 7 - id.Length;
                for (int i = 0; i < zero; i++)
                {
                    output += "0";
                }
                output += id;
                return output;
            }
            else
            {
                return providecode + id;
            }

        }
        public static string CreateImageUrl(string filename, int productid, int type)
        {
            switch (type)
            {
                case 1: return "http://images.byhand.vn/" + productid + "/" + filename;
                case 2: return "http://images.byhand.vn/Logos/" + productid + "/" + filename;
                case 4: return "http://images.byhand.vn/Backgrounds/" + productid + "/" + filename;
                default: return "http://images.byhand.vn/" + productid + "/" + filename;
            }
        }
        public static string ShortString(string source, string expectlength)
        {
            var length = Convert.ToInt32(expectlength);
            if (source.Length > length)
            {
                return source.Substring(0, length) + "...";
            }
            return source;

        }
        public static bool CheckIsChanged(Order oldorder, Order neworder)
        {
            if (oldorder.ProductID != neworder.ProductID) return true;
            if (oldorder.ToCityID != neworder.ToCityID) return true;
            if (oldorder.ToDistrictID != neworder.ToDistrictID) return true;
            if (oldorder.Quantity != neworder.Quantity) return true;
            if (!oldorder.ToAddress.Equals(neworder.ToAddress)) return true;
            if (!oldorder.ToPhone.Equals(neworder.ToPhone)) return true;
            if (!oldorder.ToEmail.Equals(neworder.ToEmail)) return true;
            if (!oldorder.ToName.Equals(neworder.ToName)) return true;
            else return false;
        }
        public static string GetCurrentTimeStamp()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssfff");
        }
        public static string GenerateId()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }
        public static string HashCode(string str)
        {
            string rethash = "";
            try
            {
                System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
                System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
                byte[] combined = encoder.GetBytes(str);
                hash.ComputeHash(combined);
                rethash = Convert.ToBase64String(hash.Hash);
            }
            catch (Exception ex)
            {
                string strerr = "Error in HashCode : " + ex.Message;
            }
            return rethash;
        }
        public static string ParseOptionValue(string optionvalue)
        {
            return optionvalue == "undefined" ? "" : optionvalue.Replace("_", ": ").Replace("-", " ");
        }
        public static void SendMail(string from, string displayfrom, string to, string subject, string body, MailPriority priority)
        {
            MailMessage msg = new MailMessage();

            // Your mail address and display name.
            // This what will appear on the From field.
            // If you used another credentials to access
            // the SMTP server, the mail message would be
            // sent from the mail specified in the From
            // field on behalf of the real sender.
            msg.From =
                new MailAddress(from, displayfrom);

            // To addresses
            msg.To.Add(to);
            //msg.To.Add(new MailAddress("friend_b@example.com", "Friend B"));

            // You can specify CC and BCC addresses also

            // Set to high priority
            msg.Priority = priority;

            msg.Subject = subject;
            msg.IsBodyHtml = true;
            // You can specify a plain text or HTML contents
            msg.Body = body;
            // In order for the mail client to interpret message
            // body correctly, we mark the body as HTML
            // because we set the body to HTML contents.

            // Attaching some data
            //msg.Attachments.Add(new Attachment("C:\\Site.lnk"));

            // Connecting to the server and configuring it
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.mandrillapp.com";
            client.Port = 587;
            client.EnableSsl = true;
            // The server requires user's credentials
            // not the default credentials
            client.UseDefaultCredentials = false;
            // Provide your credentials
            client.Credentials = new System.Net.NetworkCredential("sm@viecnho.com", "_jRYnQmX1PH5aNaV4VyPbA");
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            // Use SendAsync to send the message asynchronously
            client.Send(msg);
        }
        public static void ByHandSendMail(string to, string subject, string body)
        {
            SendMail("no-reply@byhand.vn", "ByHand.vn Thông báo.", to, subject, body, MailPriority.High);
        }
        public static string GetDomain(Uri uri)
        {
            return uri.Host;
        }
        public static string GetFolderImageGuuUpload(int guuID)
        {
            return "C:\\Website\\byhand.vn\\Files\\Guu\\"+guuID+"\\";
        }
        public static string GetImageName(string imageUrl)
        {
            var t = imageUrl.Split('/').Where(o => o.Length > 0).ToArray();
            return t[t.Length - 1];
        }
        public static string CreateImageUrl(int guuID,string imagename)
        {
            return "http://images.byhand.vn/Guu/" + guuID + "/" + imagename;
        }
    }
}
